package com.nq.fund.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nq.common.Result;
import com.nq.fund.entity.StFundKline;
import com.nq.fund.service.IStFundKlineService;
import com.nq.fund.vo.StFundKlinePo;
import com.nq.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: st_fund_kline
 * @Author: jeecg-boot
 * @Date:   2024-06-03
 * @Version: V1.0
 */
@RestController
@RequestMapping("/admin/stFundKline")
@Slf4j
public class StFundKlineController {
	@Autowired
	private IStFundKlineService stFundKlineService;
	
	/**
	 * 分页列表查询
	 *
	 * @param stFundKline
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@PostMapping(value = "/list")
	public Result<?> queryPageList(StFundKline stFundKline,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<StFundKline> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(StringUtils.isNotBlank(stFundKline.getFundCode()), "fund_code", stFundKline.getFundCode());
		queryWrapper.orderByDesc("timestamp");

		Page<StFundKline> page = new Page<StFundKline>(pageNo, pageSize);
		IPage<StFundKline> pageList = stFundKlineService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param klinePo
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(StFundKlinePo klinePo) {
		stFundKlineService.addKline(klinePo);
		return Result.OK("Added successfully！");
	}


	/**
	 * 批量新增K线
	 * @return
	 */
	@PostMapping(value = "/addBatch")
	public Result<?> addBatch(@RequestParam(name="fundCode", required=false) String fundCode,
							  @RequestParam(name="startTime", required=false) String startTime,
							  @RequestParam(name="endTime", required=true) String endTime
							  ) {
		stFundKlineService.addBatch(fundCode, startTime, endTime);
		return Result.OK("Added successfully！");
	}

	/**
	 *  编辑
	 *
	 * @param klinePo
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(StFundKlinePo klinePo) {

		stFundKlineService.editKline(klinePo);
		return Result.OK("Edited successfully!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		stFundKlineService.removeById(id);
		return Result.OK("Deleted successfully!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stFundKlineService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("Batch Delete Successfully!");
	}
	/**
	 *  一键删除
	 *
	 * @param fundCode
	 * @return
	 */
	@PostMapping(value = "/deleteKline")
	public Result<?> deleteKline(@RequestParam(name="fundCode",required=true) String fundCode) {
		this.stFundKlineService.deleteKline(fundCode);
		return Result.OK("Batch Delete Successfully!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		StFundKline stFundKline = stFundKlineService.getById(id);
		if(stFundKline==null) {
			return Result.error("No corresponding data found");
		}
		return Result.OK(stFundKline);
	}

}
